{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sentinel 2 Collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import collection, tools, ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipygee import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This means that the default collection for Landsat 1 is RAW and MSS, for Landsat 5 is SR and TM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HELPERS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_values(image):\n",
    "    print(tools.image.getValue(image, image.geometry().centroid(), 30, side='client'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sentinel 2 SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2sr = collection.Sentinel2SR()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'algorithms': {'hollstein': <function geetools.cloud_mask.applyHollstein(image, options=('cloud', 'snow', 'shadow', 'water', 'cirrus'), aerosol='B1', blue='B2', green='B3', red_edge1='B5', red_edge2='B6', red_edge3='B7', red_edge4='B8A', water_vapor='B9', cirrus='B10', swir='B11')>,\n",
       "  'scl_masks': <bound method Sentinel2.SclMasks of <geetools.collection.sentinel.Sentinel2 object at 0x7f3764289908>>},\n",
       " 'band_names': ['aerosol',\n",
       "  'blue',\n",
       "  'green',\n",
       "  'red',\n",
       "  'red_edge_1',\n",
       "  'red_edge_2',\n",
       "  'red_edge_3',\n",
       "  'nir',\n",
       "  'red_edge_4',\n",
       "  'water_vapor',\n",
       "  'swir',\n",
       "  'swir2',\n",
       "  'aerosol_thickness',\n",
       "  'water_vapor_pressure',\n",
       "  'scene_classification_map',\n",
       "  'qa10',\n",
       "  'qa20',\n",
       "  'qa60'],\n",
       " 'bands': ['B1',\n",
       "  'B2',\n",
       "  'B3',\n",
       "  'B4',\n",
       "  'B5',\n",
       "  'B6',\n",
       "  'B7',\n",
       "  'B8',\n",
       "  'B8A',\n",
       "  'B9',\n",
       "  'B11',\n",
       "  'B12',\n",
       "  'AOT',\n",
       "  'WVP',\n",
       "  'SCL',\n",
       "  'QA10',\n",
       "  'QA20',\n",
       "  'QA60'],\n",
       " 'bit_bands': ['QA10', 'QA20', 'QA60'],\n",
       " 'classification_bands': ['SCL'],\n",
       " 'cloud_cover': 'CLOUD_COVERAGE_ASSESSMENT',\n",
       " 'ee_collection': <ee.imagecollection.ImageCollection at 0x7f3764294208>,\n",
       " 'end_date': '2019-05-21',\n",
       " 'id': 'COPERNICUS/S2_SR',\n",
       " 'indices': {'evi': <function geetools.collection.Collection.evi(self, image, name='evi', renamed=False)>,\n",
       "  'nbr': <function geetools.collection.Collection.nbr(self, image, name='nbr', renamed=False)>,\n",
       "  'ndvi': <function geetools.collection.Collection.ndvi(self, image, name='ndvi', renamed=False)>},\n",
       " 'optical_bands': ['B1',\n",
       "  'B2',\n",
       "  'B3',\n",
       "  'B4',\n",
       "  'B5',\n",
       "  'B6',\n",
       "  'B7',\n",
       "  'B8',\n",
       "  'B8A',\n",
       "  'B9',\n",
       "  'B11',\n",
       "  'B12',\n",
       "  'AOT',\n",
       "  'WVP'],\n",
       " 'ranges': {'AOT': {'max': 65535, 'min': 0},\n",
       "  'B1': {'max': 10000, 'min': 0},\n",
       "  'B11': {'max': 10000, 'min': 0},\n",
       "  'B12': {'max': 10000, 'min': 0},\n",
       "  'B2': {'max': 10000, 'min': 0},\n",
       "  'B3': {'max': 10000, 'min': 0},\n",
       "  'B4': {'max': 10000, 'min': 0},\n",
       "  'B5': {'max': 10000, 'min': 0},\n",
       "  'B6': {'max': 10000, 'min': 0},\n",
       "  'B7': {'max': 10000, 'min': 0},\n",
       "  'B8': {'max': 10000, 'min': 0},\n",
       "  'B8A': {'max': 10000, 'min': 0},\n",
       "  'B9': {'max': 10000, 'min': 0},\n",
       "  'QA10': {'max': None, 'min': None},\n",
       "  'QA20': {'max': None, 'min': None},\n",
       "  'QA60': {'max': None, 'min': None},\n",
       "  'SCL': {'max': 11, 'min': 1},\n",
       "  'WVP': {'max': 65535, 'min': 0}},\n",
       " 'scales': {'AOT': 10,\n",
       "  'B1': 60,\n",
       "  'B11': 20,\n",
       "  'B12': 20,\n",
       "  'B2': 10,\n",
       "  'B3': 10,\n",
       "  'B4': 10,\n",
       "  'B5': 20,\n",
       "  'B6': 20,\n",
       "  'B7': 20,\n",
       "  'B8': 10,\n",
       "  'B8A': 20,\n",
       "  'B9': 60,\n",
       "  'QA10': 10,\n",
       "  'QA20': 20,\n",
       "  'QA60': 60,\n",
       "  'SCL': 20,\n",
       "  'WVP': 10},\n",
       " 'spacecraft': 'SENTINEL2',\n",
       " 'start_date': '2015-06-23',\n",
       " 'thermal_bands': []}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.info(s2sr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2sr_i = ee.Image(s2sr.collection.first())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B1 aerosol\n",
      "B2 blue\n",
      "B3 green\n",
      "B4 red\n",
      "B5 red_edge_1\n",
      "B6 red_edge_2\n",
      "B7 red_edge_3\n",
      "B8 nir\n",
      "B8A red_edge_4\n",
      "B9 water_vapor\n",
      "B11 swir\n",
      "B12 swir2\n",
      "AOT aerosol_thickness\n",
      "WVP water_vapor_pressure\n",
      "SCL scene_classification_map\n",
      "QA10 qa10\n",
      "QA20 qa20\n",
      "QA60 qa60\n"
     ]
    }
   ],
   "source": [
    "for band in s2sr.bands:\n",
    "    print(band.id, band.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rename"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2sr_i = s2sr_i.addBands(ee.Image.constant(0).rename('test'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2sr_renamed = s2sr.rename(s2sr_i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "06b5f1db3fcb4908a5f794a4a7eb4806",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(Accordion(children=(Output(),), _titles={'0': 'Loading...'}),))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eprint(s2sr_renamed.bandNames())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Rename collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2col = s2sr.collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2col_renamed = s2col.map(lambda img: s2sr.rename(img))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'20180326T084601_20180326T085428_T35RLP': {'MSK_CLDPRB': 0.0,\n",
       "  'MSK_SNWPRB': 0.0,\n",
       "  'TCI_B': 206.0,\n",
       "  'TCI_G': 255.0,\n",
       "  'TCI_R': 255.0,\n",
       "  'aerosol': 1674.0,\n",
       "  'aerosol_thickness': 198.0,\n",
       "  'blue': 2020.0,\n",
       "  'green': 2866.0,\n",
       "  'nir': 5023.0,\n",
       "  'qa10': 0.0,\n",
       "  'qa20': 0.0,\n",
       "  'qa60': 0.0,\n",
       "  'red': 3926.0,\n",
       "  'red_edge_1': 4244.0,\n",
       "  'red_edge_2': 4408.0,\n",
       "  'red_edge_3': 4633.0,\n",
       "  'red_edge_4': 4913.0,\n",
       "  'scene_classification_map': 5.0,\n",
       "  'swir': 6598.0,\n",
       "  'swir2': 5376.0,\n",
       "  'water_vapor': 4999.0,\n",
       "  'water_vapor_pressure': 446.0},\n",
       " '20180326T084601_20180326T085428_T35RLQ': {'MSK_CLDPRB': None,\n",
       "  'MSK_SNWPRB': None,\n",
       "  'TCI_B': None,\n",
       "  'TCI_G': None,\n",
       "  'TCI_R': None,\n",
       "  'aerosol': None,\n",
       "  'aerosol_thickness': None,\n",
       "  'blue': None,\n",
       "  'green': None,\n",
       "  'nir': None,\n",
       "  'qa10': None,\n",
       "  'qa20': None,\n",
       "  'qa60': None,\n",
       "  'red': None,\n",
       "  'red_edge_1': None,\n",
       "  'red_edge_2': None,\n",
       "  'red_edge_3': None,\n",
       "  'red_edge_4': None,\n",
       "  'scene_classification_map': None,\n",
       "  'swir': None,\n",
       "  'swir2': None,\n",
       "  'water_vapor': None,\n",
       "  'water_vapor_pressure': None}}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools.imagecollection.getValues(s2col_renamed.limit(2), ee.Image(s2col.first()).geometry().centroid(), 20, side='client')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SCL Mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = s2sr.SclMasks(s2sr_i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = Map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "031aaedfb5884397a936fd8e7f97348c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'max_zoom': 19, 'url': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 'attribution': 'Map …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ad0130dfb5214cf9a76fc44d76c118ff",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Map.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### renamed image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = ee.Geometry.Point([-71, -41])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2col_renamed = s2col_renamed.filterBounds(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2sr_renamed = s2col_renamed.first()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "vis = s2sr.visualization('NSR', True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(s2sr_renamed, vis, 'S2 SR Renamed NSR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.centerObject(s2sr_renamed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SCL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "visSCL = s2sr.visualization('SCL', True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(s2sr_renamed, visSCL, 'SCL')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Masks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'qa60': ['cloud', 'cirrus']}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2sr.bitOptions(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "masked = s2sr.applyMask(s2sr_renamed, 'qa60', renamed=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(masked, vis, 'Masked')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hollstein"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "s2toa = collection.Sentinel2TOA()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = ee.Image(s2toa.collection.filterBounds(p).filterMetadata(s2toa.cloud_cover, 'less_than', 50).first())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(i, s2toa.visualization('NSR'), 'TOA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "holl = s2toa.algorithms['hollstein']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function applyHollstein in module geetools.cloud_mask:\n",
      "\n",
      "applyHollstein(image, options=('cloud', 'snow', 'shadow', 'water', 'cirrus'), aerosol='B1', blue='B2', green='B3', red_edge1='B5', red_edge2='B6', red_edge3='B7', red_edge4='B8A', water_vapor='B9', cirrus='B10', swir='B11')\n",
      "    Apply Hollstein mask\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(holl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "iholl = holl(i, ['cloud', 'shadow', 'snow'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(iholl, s2toa.visualization('NSR'), 'Masked TOA')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
